package com.mall.b2c.hessian.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mall.b2c.bean.ConsultBean;
import com.mall.b2c.bean.SiteNoticeBean;
import com.mall.b2c.domain.member.ConsultDomain;
import com.mall.b2c.domain.member.SiteNoticeDomain;
import com.mall.b2c.enums.ConsultType;
import com.mall.b2c.hessian.MemberExtendHessian;
import com.mall.b2c.service.ConsultService;
import com.mall.b2c.service.GoodsService;
import com.mall.b2c.service.SiteNoticeService;
import com.mall.b2c.service.StockNoticeService;
import com.mall.common.bean.Pager;
import com.mall.common.exception.NoticeException;
import com.mall.common.hessian.impl.BaseHessianImpl;
import com.mall.common.hibernate.Finder;

@Service("b2cMemberExtendHessian")
public class MemberExtendHessianImpl extends BaseHessianImpl implements MemberExtendHessian{
	
	@Resource SiteNoticeService siteNoticeService;
	@Resource StockNoticeService stockNoticeService;
	@Resource GoodsService goodsService;
	@Resource ConsultService consultService;
	
	@Override
	public String getSiteNoticePageData(Long memberId, Integer page,
			Integer size) {
		String hql = "select new com.mall.b2c.bean.SiteNoticeBean(bean) from SiteNoticeDomain bean"
				+" where bean.isDelete = false and ( bean.member.id is null and bean.noticeText.id not in (select n.noticeText.id from SiteNoticeDomain n where n.member.id = :memberId)"
				+" or bean.member.id = :memberId ) order by bean.isRead asc,bean.createDate desc";
		Finder finder = Finder.create(hql);
		finder.setParam("memberId", memberId);
		Pager pager = newPager(page, size);
		pager = siteNoticeService.findByPagerForHessian(pager, finder);
		return objToJson(pager);
	}

	@Override
	public String getSiteNoticeData(Long noticeTextId, Long memberId) {
		SiteNoticeDomain siteNotice = siteNoticeService.view(noticeTextId, memberId);
		return objToJson(new SiteNoticeBean(siteNotice));
	}

	@Override
	public void deleteSiteNotice(Long[] noticeTextIds, Long memberId) {
		siteNoticeService.delete(noticeTextIds, memberId);
	}

	@Override
	public String getStockNoticePageData(Long memberId, Integer page,
			Integer size) {
		String hql = "select new com.mall.b2c.bean.StockNoticeBean(bean) from StockNoticeDomain bean where bean.member.id = :memberId";
		Finder finder = Finder.create(hql);
		finder.setParam("memberId", memberId);
		Pager pager = newPager(page, size);
		pager = stockNoticeService.findByPagerForHessian(pager, finder);
		return objToJson(pager);
	}

	@Override
	public void saveStockNotice(Long goodsId, Integer quantity, String memo,
			Long memberId) {
		stockNoticeService.register(goodsId, quantity, memo, memberId);
	}

	@Override
	public void deleteStockNotice(Long[] stockNoticeIds, Long memberId) {
		stockNoticeService.delete(stockNoticeIds, memberId);
	}

	@Override
	public String getHistoryGoods(String idsStr) {
		return listToJson(goodsService.getHistoryGoods(idsStr));
	}

	@Override
	public String getConsultPagerData(Long memberId, Integer consultType, Integer page, Integer size) {
		ConsultType[] types = ConsultType.values();
		if(consultType != null && consultType >= types.length){
			throw new NoticeException("咨询类型不存在");
		}
		String hql = "select new com.mall.b2c.bean.ConsultBean(bean) from ConsultDomain bean"
				   + "  where bean.member.id = :memberId and bean.deleteStatus = false";
		Finder finder = Finder.create(hql);
		finder.setParam("memberId", memberId);
		if(consultType != null){
			finder.append(" and bean.type = :type");
			finder.setParam("type", types[consultType]);
		}
		finder.append(" order by bean.createDate desc");
		Pager pager = newPager(page, size);
		pager = consultService.findByPagerForHessian(pager, finder);
		return objToJson(pager);
	}

	@Override
	public String getConsultData(Long consultId, Long memberId) {
		String hql = "select new com.mall.b2c.bean.ConsultBean(bean) from ConsultDomain bean"
					+ " where bean.member.id = :memberId"
					+ " and bean.id = :consultId ";
		Finder finder = Finder.create(hql);
		finder.setParam("memberId", memberId);
		finder.setParam("consultId", consultId);
		return objToJson(finder.findUniqueByHQL());
	}
	
	@Override
	public String saveConsultData(ConsultBean bean, Long memberId) {
		ConsultDomain consult = consultService.saveConsult(bean, memberId);
		return objToJson(new ConsultBean(consult));
	}
	
	@Override
	public void saveCorrectData(Long goodsId, String content, Long memberId) {
		consultService.saveCorrect(goodsId, content, memberId);
	}
	
	@Override
	public void deleteConsultData(Long[] consultIds, Long memberId) {
		consultService.deleteConsult(consultIds, memberId);
	}
	
}
